package simplyrugby.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import simplyrugby.common.Result;
import simplyrugby.entity.Season;
import simplyrugby.service.SeasonService;

import java.util.List;

@RestController
@RequestMapping("/api/season")
@RequiredArgsConstructor
public class SeasonController {
    private final SeasonService seasonService;


    @PostMapping
    public Result add(@RequestBody Season season) {
        boolean save = seasonService.save(season);
        return Result.success().data(save);
    }

    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable String id) {
        boolean b = seasonService.removeById(id);
        return Result.success().data(b);
    }

    @PutMapping
    public Result updateById(@RequestBody Season season) {
        boolean flag = seasonService.updateById(season);
        return flag ? Result.success() : Result.fail();
    }


    @GetMapping("/{id}")
    public Result details(@PathVariable String id) {
        Season season = seasonService.getById(id);
        return Result.success().data(season);
    }

    @GetMapping("/list")
    public Result list() {
        List<Season> list = seasonService.list();
        return Result.success().data(list);
    }

    @PostMapping("/page")
    public Result page(@RequestParam(required = false) Integer pageNum,
                       @RequestParam(required = false) Integer pageSize,
                       @RequestBody Season season
    ) {
        Page<Season> page = seasonService.list(pageNum, pageSize, season);
        return Result.success().data(page);
    }
}

